package com.ruoyi.system.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.QRCodeUtil;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.domain.DragSysConfig;
import com.ruoyi.system.domain.MedicineDetail;
import com.ruoyi.system.service.IDragSysConfigService;
import com.ruoyi.system.service.IMedicineDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MedicineInfoMapper;
import com.ruoyi.system.domain.MedicineInfo;
import com.ruoyi.system.service.IMedicineInfoService;
import org.springframework.transaction.annotation.Transactional;

import javax.imageio.ImageIO;

/**
 * meidicineInfoService业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-17
 */
@Service
public class MedicineInfoServiceImpl implements IMedicineInfoService 
{
    @Autowired
    private MedicineInfoMapper medicineInfoMapper;
    @Autowired
    private IMedicineDetailService medicineDetailService;


    @Autowired
    private IDragSysConfigService dragSysConfigService;

    /**
     * 查询meidicineInfo
     * 
     * @param id meidicineInfo主键
     * @return meidicineInfo
     */
    @Override
    public MedicineInfo selectMedicineInfoById(Long id)
    {
        MedicineInfo  medicineInfo= medicineInfoMapper.selectMedicineInfoById(id);
        MedicineDetail medicineDetail= new MedicineDetail();
        medicineDetail.setMedicineId(id);
        List<MedicineDetail> medicineDetails= medicineDetailService.selectMedicineDetailList(medicineDetail);
        medicineInfo.setMedicineDetails(medicineDetails);
        return medicineInfo;
    }

    /**
     * 查询meidicineInfo列表
     * 
     * @param medicineInfo meidicineInfo
     * @return meidicineInfo
     */
    @Override
    public List<MedicineInfo> selectMedicineInfoList(MedicineInfo medicineInfo)
    {
        return medicineInfoMapper.selectMedicineInfoList(medicineInfo);
    }

    /**
     * 新增meidicineInfo
     * 
     * @param medicineInfo meidicineInfo
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMedicineInfo(MedicineInfo medicineInfo) throws IOException {
        medicineInfo.setCreateTime(DateUtils.getNowDate());
        DragSysConfig dragSysConfig= dragSysConfigService.selectDragSysConfigById(1l);
        medicineInfo.setVoiceType(dragSysConfig.getXfVoiceName());
        int result= medicineInfoMapper.insertMedicineInfo(medicineInfo);
        //二维码处理
//        String filePath = RuoYiConfig.getUploadPath();
//        BufferedImage bufferedImage = QRCodeUtil.createImage("UTF-8","yum"+"", 400, 400);
//        File outputFile = new File(filePath+File.separator+"QR"+File.separator+DateUtils.getDate()+ File.separator+medicineInfo.getId()+".jpg");
//        ImageIO.write(bufferedImage, "JPEG", outputFile);
        // 上传并返回新文件名称
        MedicineDetail medicineDetail=new MedicineDetail();
        //插入详情
        if(medicineInfo.getMedicineDetails()!=null){
            for(int i=0;i<medicineInfo.getMedicineDetails().size();i++){
                medicineDetail=medicineInfo.getMedicineDetails().get(i);
                medicineDetail.setMedicineId(medicineInfo.getId());
                medicineDetail.setOrderNumber(Long.valueOf(i+""));
                medicineDetailService.insertMedicineDetail(medicineDetail);
            }
        }
        //二维码处理
        generateQrUrl(medicineInfo);
        return medicineInfoMapper.updateMedicineInfo(medicineInfo);
    }

    //
     //修改药品详情顺序
     @Override
     @Transactional
     public int updateMedicineDetialNumber(MedicineInfo medicineInfo)
     {
         MedicineDetail medicineDetail=new MedicineDetail();
         //插入详情
         if(medicineInfo.getMedicineDetails()!=null){
             for(int i=0;i<medicineInfo.getMedicineDetails().size();i++){
                 medicineDetail.setMedicineId(medicineInfo.getId());
                 medicineDetail.setOrderNumber(Long.valueOf(i+""));
                 medicineDetailService.updateMedicineDetail(medicineDetail);
             }
         }
         return medicineInfo.getMedicineDetails().size();
     }

    /**
     * 修改meidicineInfo
     * 
     * @param medicineInfo meidicineInfo
     * @return 结果
     */
    @Override
    @Transactional
    public int updateMedicineInfo(MedicineInfo medicineInfo) throws IOException {
        medicineInfo.setUpdateTime(DateUtils.getNowDate());
        MedicineDetail medicineDetail=new MedicineDetail();
        medicineDetail.setMedicineId(medicineInfo.getId());
        // 先删除之前的。防止新增了说明，数据库没有此条记录，无法修改
        medicineDetailService.deleteMedicineDetailByMidicineId(medicineDetail);
        if(medicineInfo.getMedicineDetails()!=null){
            for(int i=0;i<medicineInfo.getMedicineDetails().size();i++){
                medicineDetail =medicineInfo.getMedicineDetails().get(i);
                medicineDetail.setMedicineId(medicineInfo.getId());
                medicineDetail.setOrderNumber(Long.valueOf(i+""));
                medicineDetailService.insertMedicineDetail(medicineDetail);
            }
        }
       // DragSysConfig dragSysConfig= dragSysConfigService.selectDragSysConfigById(1l);
       // medicineInfo.setVoiceType(dragSysConfig.getXfVoiceName());
        generateQrUrl(medicineInfo);
        return medicineInfoMapper.updateMedicineInfo(medicineInfo);
    }

    @Override
    public int updateOnlyMedicineInfo(MedicineInfo medicineInfo) {
        return medicineInfoMapper.updateMedicineInfo(medicineInfo);
    }

    public void generateQrUrl(MedicineInfo medicineInfo) throws IOException {
        ///
        String filePath = RuoYiConfig.getUploadPath();
        String qrContenturl="https://slhsms.kpc.com.cn/h5/?id="+medicineInfo.getId();
        BufferedImage bufferedImage = QRCodeUtil.createImage("UTF-8",qrContenturl, 400, 400);
        String url="QR/"+DateUtils.getDate();
        filePath=filePath+File.separator+url;
        File directory = new File(filePath);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        filePath=filePath+File.separator+medicineInfo.getId()+".jpg";
        File outputFile = new File(filePath);
        ImageIO.write(bufferedImage, "JPEG", outputFile);
        medicineInfo.setQrCodeUrl("/profile/"+"upload/" +url+"/"+medicineInfo.getId()+".jpg");
    }



    /**
     * 批量删除meidicineInfo
     * 
     * @param ids 需要删除的meidicineInfo主键
     * @return 结果
     */
    @Override
    public int deleteMedicineInfoByIds(Long[] ids)
    {
        return medicineInfoMapper.deleteMedicineInfoByIds(ids);
    }


    @Override
    public List<MedicineInfo> selectMedicineInfoByIds(Long[] ids)
    {
        return medicineInfoMapper.selectMedicineInfoByIds(ids);
    }



    /**
     * 删除meidicineInfo信息
     * 
     * @param id meidicineInfo主键
     * @return 结果
     */
    @Override
    public int deleteMedicineInfoById(Long id)
    {
        return medicineInfoMapper.deleteMedicineInfoById(id);
    }
}
